using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Chapter8CustomControlsCS
{
    
    [InitializationEvent("Startup")]
    [DefaultProperty("ConnectionFailed")]
    [ToolboxData("<{0}:Delegates runat=server></{0}:Delegates>")]
    public class Delegates : WebControl
    {
        bool bolDirty;
        delegate void StartupEventHandler(Object sender, EventArgs e);
        event StartupEventHandler Startup;

        delegate bool Sample1(string FirstParameter, int SecondParameter);
        delegate void Sample2();

        [Description("A test event")]
        [Category("Action")]
        public delegate void ConnectionFailedEventHandler(Object sender, ConnectionFailedEventArgs e);
        public event ConnectionFailedEventHandler ConnectionFailed;

        private static readonly object ConnectionFailedKey = new object();

        public event ConnectionFailedEventHandler ConnectionEvent
        {
            add
            {
                this.Events.AddHandler(ConnectionFailedKey, value);
            }
            remove
            {
                this.Events.RemoveHandler(ConnectionFailedKey, value);
            }
        }
        
       void Test() 
        { Boolean bolResult;
          Sample1 del1 = new Sample1(CalledRoutine);
          bolResult = del1.Invoke("Peter", 55);

          try
          {
          }
          catch (System.Data.OleDb.OleDbException ex)
          {
              ConnectionFailedEventArgs e = new ConnectionFailedEventArgs();
              e.ErrorCode = ex.ErrorCode;
              if (ConnectionFailed != null)
              {
                  OnConnectionFailed(e);
              }
          }
       }

       void OnConnectionFailed(ConnectionFailedEventArgs e)
       {
        ConnectionFailedEventHandler ConnectionFailed =
               (ConnectionFailedEventHandler)Events[ConnectionFailedKey];

         ConnectionFailed(this, e);
         switch (e.Command)
         {
          case "Retry":
                 break;
         }
       }

        bool CalledRoutine(string parm1, int parm2)
        {
            return true;
        }

        protected override void CreateChildControls()
        {
            System.Web.UI.WebControls.Button btn;
            btn = new System.Web.UI.WebControls.Button();
            btn.ID = "btnSubmit";
            btn.CommandName = btn.ID;
            btn.CommandArgument = "Click";

            System.EventHandler ev;
            ev = new System.EventHandler(this.PassAnEvent);
            btn.Click += ev;
            this.Controls.Add(btn);
        }

        protected void PassAnEvent(Object sender, EventArgs e)
        {
            this.RaiseBubbleEvent(this, e);
        }

        protected override bool OnBubbleEvent(object source, System.EventArgs args)
        {
         System.Web.UI.WebControls.Button cbtn;
         System.Web.UI.WebControls.CommandEventArgs cent;

         switch (source.GetType().Name)
         {
             case "WebControl.Button":
                 cbtn = (System.Web.UI.WebControls.Button)source;
                 switch (cbtn.CommandName)
                 {
                     case "btnSubmit":
                         cent = (System.Web.UI.WebControls.CommandEventArgs)args;
                         if (cent.CommandArgument.ToString() == "Click")
                         {
                             this.Dirty = true;
                         }
                         else
                         {
                             this.Dirty = false;
                         }
                         break;
                     //...code for other buttons...
                 };
                 break;
             case "WebControl.TextBox":
                 //...etc. ...
                 break;
         }
         return true;            
        }

        bool Dirty
        {
            get 
            {
                return bolDirty;
            }
            set
            {
                bolDirty = value;
            }
        }
        
     protected override void AddedControl(System.Web.UI.Control control, int index)
     {
         if (control.GetType().Name == "TextBox")
         {
             control.ID = "NewText" + index;
         }
     }
   }   
  }

